package br.com.statizo.financeiro.spring.listener;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.snoghertframework.swt.listener.DialogListener;
import org.snoghertframework.swt.util.impl.LayoutUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import br.com.statizo.financeiro.view.ResumoComposite;


public class ApplicationInitializer implements ApplicationListener<ContextRefreshedEvent>{

	
	@Autowired
	private Shell shell;
	
	@Autowired
	private LayoutUtil layoutUtil;
	
	private TabFolder tabFolder;
	
	@Autowired
	private DialogListener lancamentoDialogListener;
	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {

		 shell.setLayout(new FormLayout());
		 
		 tabFolder = new TabFolder(shell, SWT.NONE);
		 layoutUtil.setFormLayout(tabFolder);
		 layoutUtil.setFormData(tabFolder, 10, 0, 100, 100);
		 
		 createCalendarComposite();
		 createResumoComposite();
		 createLancamentoComposite();

		
	}


	protected void createCalendarComposite(){

		 Group group = new Group(tabFolder,SWT.NONE);
		 layoutUtil.setFormLayout(group);
		 layoutUtil.setFormData(group, 5, 5, 20, 90);
		 group.setText("Período");
		 
		 List calendarList = new List(group, SWT.NONE | SWT.SINGLE);
		 layoutUtil.setFormData(calendarList, 2, 2, 98, 98);
		 calendarList.setItems(new String[]{"Item 1","Item 2"});
	}
	
	protected void createResumoComposite(){
		
		 TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		 tabItem.setText("Resumo");
		 
		 ResumoComposite resumo = new ResumoComposite(tabFolder, SWT.NONE);
		 layoutUtil.setFormData(resumo, 2, 30, 90, 90);
		 tabItem.setControl(resumo);
		 

		 
		 
		
	}
	
	
	protected void createLancamentoComposite(){
		
		 TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		 tabItem.setText("Lançamento");

		 Composite lancComp = new Composite(tabFolder, SWT.NONE);
		 layoutUtil.setFormLayout(lancComp);
		 layoutUtil.setFormData(lancComp, 10, 30, 90, 90);

		 tabItem.setControl(lancComp);
		 
		 
		 Composite actionComposite = new Composite(lancComp,SWT.NONE);
		 layoutUtil.setFormLayout(actionComposite);
		 layoutUtil.setFormData(actionComposite, 10, 30, 90, 15);
		 
		 Button button = new Button(actionComposite,SWT.NONE);
		 layoutUtil.setFormData(button, 2, 2, 20, 50);
		 button.setText("add (+)");
		 button.addMouseListener(lancamentoDialogListener);

		 
		 Label separator = new Label(actionComposite,SWT.SEPARATOR | SWT.HORIZONTAL);
		 layoutUtil.setFormData(separator, 99, 0, 100, 100);
		 
		 Table table = new Table(lancComp, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.FULL_SELECTION);
		 table.setHeaderVisible(true);
		 table.setLinesVisible(true);
		 
		 layoutUtil.setFormData(table, 18, 30, 90, 90);
		 
		 createColumn("Dt. Dívida",table);
		 createColumn("Dt. Vcto.",table);

		 TableItem item = new TableItem(table, SWT.NONE);
		 item.setText(new String[]{"A","B"});
		 
		 
	}
	
	

	
	protected void createColumn(String header, Table table){
		
		 TableColumn tableColumn = new TableColumn(table, SWT.BORDER | SWT.NO_REDRAW_RESIZE);		 
		 tableColumn.setText(header);
		 tableColumn.setResizable(true);
		 tableColumn.pack();
		
	}
	
	

}
